cc.Class({

    extends: cc.Component,
    properties: {
    },
    onLoad() {
        this.initView();
        this.initData();
        this.initTestData();
    },

    /**
     * 初始化界面
     */
    initView() {
        this.initComponent();
    },
    /**
     * 初始化数据
     */
    initData() {
        this.score = 0;
        this.resetSysNum();
        this.resultFlag = 0;

        if(window.touchViewData.hasFive){
            this.btnTouch4.node.active = true;
        }

    },

    resetSysNum() {
        var ran = Math.ceil(Math.random() * (this.score + 3));
        ran += 1;
        this.sysNum = ran;
        this.playNum = 0;
    },

    /**
     * 生成测试数据
     */
    initTestData() {
    },
    initComponent: function () {
        window.GameViewData.touchView = this;

        // this.button1 = cc.find("button1", this.node).getComponent(cc.Button);
        this.button2 = cc.find("button2", this.node).getComponent(cc.Button);
        this.button3 = cc.find("button3", this.node).getComponent(cc.Button);
        this.btnTouch = cc.find("btnTouch", this.node).getComponent(cc.Button);
        this.btnTouch2 = cc.find("btnTouch2", this.node).getComponent(cc.Button);
        this.btnTouch4 = cc.find("btnTouch4", this.node).getComponent(cc.Button);

        this.sysSprite = cc.find("sysSprite", this.node).getComponent(cc.Sprite);
        this.sysNumText = cc.find("sysSprite/sysNumText", this.node).getComponent(cc.RichText);
        this.playSprite = cc.find("playSprite", this.node).getComponent(cc.Sprite);
        this.playNumText = cc.find("playSprite/playNumText", this.node).getComponent(cc.RichText);

        this.lbScore = cc.find("lbScore", this.node).getComponent(cc.Label);

        this.resultPanle = cc.find("resultPanle", this.node);
        this.lbResult = cc.find("resultPanle/lbResult", this.node).getComponent(cc.Label);
        this.lbReson = cc.find("resultPanle/lbReson", this.node).getComponent(cc.Label);
        this.button1 = cc.find("resultPanle/button1", this.node).getComponent(cc.Button);
        this.lbBtn1 = cc.find("resultPanle/button1/lbBtn1", this.node).getComponent(cc.Label);

    },

    showResult(resultFlag, result, reson, btn1String) {
        this.resultFlag = resultFlag;
        this.lbResult.string = result;
        this.lbReson.string = reson;
        this.lbBtn1.string = btn1String;
        this.resultPanle.active = true;
        this.button2.node.active = false;
    },

    update: function (dt) {
        this.refreshTokenView();
    },
    refreshTokenView() {
        this.sysNumText.string = this.sysNum + "";
        this.playNumText.string = this.playNum + "";
        this.lbScore.string = "当前分数：" + this.score + "";
    },

    onEnable: function () {
        this.addListener();
        this.showBannerAd();

    },
    onDisable: function () {
        this.removeListener();
        this.destroyBannerAd();
    },
    addListener: function () {
        this.button1Event = this.button1.node.on(cc.Node.EventType.TOUCH_END, this.button1Click.bind(this));
        this.button2Event = this.button2.node.on(cc.Node.EventType.TOUCH_END, this.button2Click.bind(this));
        this.button3Event = this.button3.node.on(cc.Node.EventType.TOUCH_END, this.button3Click.bind(this));
        this.btnTouchEvent = this.btnTouch.node.on(cc.Node.EventType.TOUCH_END, this.btnTouchClick.bind(this));
        this.btnTouch2Event = this.btnTouch2.node.on(cc.Node.EventType.TOUCH_END, this.btnTouch2Click.bind(this));
        this.btnTouch4Event = this.btnTouch4.node.on(cc.Node.EventType.TOUCH_END, this.btnTouch4Click.bind(this));

    },

    removeListener: function () {
        this.button1.node.off(cc.Node.EventType.TOUCH_END, this.button1Event);
        this.button2.node.off(cc.Node.EventType.TOUCH_END, this.button2Event);
        this.button3.node.off(cc.Node.EventType.TOUCH_END, this.button3Event);
        this.btnTouch.node.off(cc.Node.EventType.TOUCH_END, this.btnTouchEvent);
        this.btnTouch2.node.off(cc.Node.EventType.TOUCH_END, this.btnTouch2Event);
        this.btnTouch4.node.off(cc.Node.EventType.TOUCH_END, this.btnTouch4Event);
        // window.EventBus.removeListener(window.GameConstant.EventTypeID.OnResultChange);
    },
    button1Click: function () {
        if (this.resultFlag == 1) {
            this.score += this.playNum;
            if (this.score > 200) {
                this.btnTouch4.node.active = true;
            }

        } else {
            let score = this.score;
            if (CC_WECHATGAME) {
                window.wx.postMessage({
                    messageType: 3,
                    MAIN_MENU_NUM: "x1",
                    score: score,
                });
            } else {
                console.log("提交得分: x1 : " + score)
            }
            this.score = 0;
        }

        this.resultPanle.active = false;
        this.button2.node.active = true;
        this.resetSysNum();
    },
    btnTouchClick: function () {
        this.playNum++;
    },
    btnTouch2Click: function () {
        this.playNum += 2;
    },
    btnTouch4Click: function () {
        this.playNum += 5;
    },
    button2Click: function () {
        if (this.playNum == this.sysNum) {
            console.log("成功，进行下一关");
            this.showResult(1, "恭喜,成功", "", "下一关");
            return;
        } else if (this.playNum > this.sysNum) {
            console.log("失败，你的太大了。正在上传玩家数据...");
            this.showResult(16, "可惜，失败", "你的太大了", "重来");

        } else if (this.playNum < this.sysNum) {
            console.log("失败，你的太小了，正在上传玩家数据...");
            this.showResult(8, "可惜，失败", "你的太小了", "重来");
        }
    },
    button3Click: function () {
        window.UIManager.showWindow(window.GameConstant.LayerEnum.UI, window.GameConstant.PrefabNameEnum.GAME_START_WIND);
        window.UIManager.hideWindow(window.GameConstant.PrefabNameEnum.GAME_TOUCH_WIND);
    },
    destroyBannerAd() {
        if (!CC_WECHATGAME) {
            console.log("销毁广告1");
            return;
        }
        if(window.ad.bannerSecond != null){
            window.ad.bannerSecond.destroy();
        }
    },
    showBannerAd() {
        if (!CC_WECHATGAME) {
            console.log("显示广告1");
            return;
        }
        this.destroyBannerAd();
        var winSize = wx.getSystemInfoSync();
        var bannerHeight = 50;
        var bannerWidth = 300;
        window.ad.bannerSecond = wx.createBannerAd({
            adUnitId: window.common.wx.ad.bannerSecondId,
            style: {
                // left: (winSize.windowWidth - bannerWidth) / 2,
                left: 0,
                top: winSize.windowHeight - bannerHeight,
                width: 300
            }
        })
        window.ad.bannerSecond.show();
        window.ad.bannerSecond.onResize(res => {
            window.ad.bannerSecond.style.top = winSize.windowHeight - window.ad.bannerSecond.style.realHeight;
        })

        window.ad.bannerSecond.onError(function (err) {
            console.log(err);
        });
        window.ad.bannerSecond.onLoad(function () {
            console.log("banner 广告加载成功");
        });
    },
});
